package com.lin.lostandfound.service.impl;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lin.lostandfound.mapper.LoginLogMapper;
import com.lin.lostandfound.mapper.UserMapper;
import com.lin.lostandfound.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lin.lostandfound.bean.LoginLog;
import com.lin.lostandfound.bean.User;
import com.lin.lostandfound.service.IUserService;

@Service("userService")
public class UserService implements IUserService {
	@Autowired
	private UserMapper mapper;
	@Autowired
	private LoginLogMapper loginLogMapper;
	@Autowired
	private StringUtil util;
	@Override
	public boolean addUser(User user) {
		
		return mapper.insert(user)!=-1;
	}

	@Override
	@Transactional
	public boolean addUser(List<User> list) {
		for (User user : list) {
			mapper.insert(user);
		}
		return true;
	}

	@Override
	public boolean modify(User user) {
		
		return mapper.updateById(user)!=-1;
	}
	
	@Override
	public boolean modify(List<User> list) {
		for (User user : list) {
			mapper.updateById(user);
		}
		return true;
	}

	@Override
	public boolean remove(long userId) {
		
		return mapper.deleteById(userId)!=-1;
	}
	
	@Override
	public boolean remove(String userId) {
		String[] id = userId.split(",");
		
		int[] uid = new int[id.length];
		for(int i = 0 ;i < id.length; i++){
			uid[i] = Integer.parseInt(id[i]);
		}
		
		return mapper.deleteBatchIds(Arrays.asList(uid))!=-1;
	}
	
	// 设置（撤销）管理员
	@Override
	public boolean adminSetOrCancel(Integer userId, int state) {
		User user = new User();
		user.setId(userId);
		user.setState(state);
		int i = mapper.updateById(user);
		return i!=-1;
	}
	
	// 批量锁定（解锁）用户
	@Override
	public boolean lockOrUnlock(String userId, int state) {
		String[] id = userId.split(",");
		User user = new User();
		for (int i = 0; i < id.length; i++) {
			user.setId(Integer.parseInt(id[i]));
			user.setState(state);
			mapper.updateById(user);
		}
		return true;
	}

	// 锁定用户
	@Override
	public boolean lock(Integer id, int state) {
		User user = new User();
		user.setId(id);
		user.setState(state);
		int i = mapper.updateById(user);
		return i!=-1;
	}
	
	// 批量锁定用户
	@Override
	public boolean lock(String userId, int state) {
		String[] id = userId.split(",");
		User user = new User();
		for (int i = 0; i < id.length; i++) {
			user.setId(Integer.parseInt(id[i]));
			user.setState(state);
			mapper.updateById(user);
		}
		return true;
	}

	// 解锁用户
	@Override
	public boolean unlock(Integer id, int state) {

		User user = new User();
		user.setId(id);
		user.setState(state);
		int i = mapper.updateById(user);
		return i!=-1;
	}
	
	// 批量解锁用户
	@Override
	public boolean unlock(String userId, int state) {
		String[] id = userId.split(",");
		User user = new User();
		for (int i = 0; i < id.length; i++) {
			user.setId(Integer.parseInt(id[i]));
			user.setState(state);
			mapper.updateById(user);
		}
		return true;
	}

	@Override
	public User login(String userName, String password, int authority) {
		QueryWrapper<User> wrapper = new QueryWrapper<>();


		wrapper.eq("userName", userName);
		wrapper.eq("password", password);
		wrapper.ge("state", authority);

		return mapper.selectOne(wrapper);
	}

	@Override
	public User queryUserById(long id) {
		
		return mapper.selectById(id);
	}

	// 根据关键字查询所有用户
	@Override
	public List<User> queryAllByKeywords(String keywords, Long currentPage, Long pageSize) {

		Page<User> page_ = new Page<>(currentPage,pageSize);
		QueryWrapper<User> wrapper = new QueryWrapper<>();

		if(util.notnull(keywords)){
			wrapper.like("userName",keywords);
		}
		IPage<User> page=mapper.selectPage(page_,wrapper);

		return page.getRecords();
	}
	
	// 查询所有统计数量
	@Override
	public Integer queryAllCount(String key) {


		return mapper.getusercount(key);
	}

	// 查询所有用户
	@Override
	public Map<String, Object> queryAll(String key, Integer pageIndex, Integer pageSize, String sortField, String sortOrder) {


		Page<User> page_ = new Page<>(pageIndex,pageSize);
		QueryWrapper<User> wrapper = new QueryWrapper<>();
		wrapper.ne("state",3);
		if(util.notnull(key)){

			wrapper.like("userName",key);
			wrapper.or();
			wrapper.like("grade",key);
			wrapper.or();
			wrapper.like("name",key);
		}
		if (util.notnull(sortField)) {
			if(sortOrder.equals("desc")){
				wrapper.orderByDesc(sortField);
			}else{
				wrapper.orderByAsc(sortField);
			}
		}else{
			wrapper.orderByDesc("publishTime");
		}
		IPage<User> page=mapper.selectPage(page_,wrapper);


		Map<String, Object> map = new HashMap<String, Object>();
		map.put("data", page.getRecords());
		map.put("total", page.getTotal());

		return map;
	}

	@Override
	public List<User> queryAll(Long currentPage, Long pageSize) {

		Page<User> page_ = new Page<>(currentPage,pageSize);

		IPage<User> page=mapper.selectPage(page_,null);

		return page.getRecords();
	}
	
	// 保存登录日志信息
	@Override
	public boolean addLoginlog(LoginLog loginLog) {
		
		return loginLogMapper.insert(loginLog)!=-1;
	}
	
	// 查询所有统计数量
	public Integer queryAllLoginlogCount(String key) {
		QueryWrapper<LoginLog> wrapper = new QueryWrapper<>();
		if(util.notnull(key)){
			wrapper.like("userName",key);

		}
		int i = loginLogMapper.selectCount(wrapper);
		return i;
	}
	
	// 查询所有登录日志信息
	@Override
	public Map<String, Object> queryAllLoginlog(String key, Integer pageIndex, Integer pageSize, String sortField, String sortOrder) {

		Page<LoginLog> page_ = new Page<>(pageIndex,pageSize);
		QueryWrapper<LoginLog> wrapper = new QueryWrapper<>();
		if(util.notnull(key)){
			wrapper.like("userName",key);

		}
		if (util.notnull(sortField)) {
			if(sortOrder.equals("desc")){
				wrapper.orderByDesc(sortField);
			}else{
				wrapper.orderByAsc(sortField);
			}
		}else{
			wrapper.orderByDesc("loginTime");
		}
		IPage<LoginLog> page=loginLogMapper.selectPage(page_,wrapper);


		Map<String, Object> map = new HashMap<String, Object>();
		map.put("data", page.getRecords());
		map.put("total", page.getTotal());

		return map;
	}
	
	// 根据用户id查询统计所有（分页）
	@Override
	public long queryLoginlogByUidCount(Integer uid) {

		QueryWrapper<LoginLog> wrapper = new QueryWrapper<>();

		wrapper.eq("uid",uid);


		int i = loginLogMapper.selectCount(wrapper);
		return i;
	}
	
	// 根据uid查询登录日志信息
	@Override
	public List<LoginLog> queryLoginlogByUid(Integer uid, Integer pageIndex, Integer pageSize) {

		Page<LoginLog> page_ = new Page<>(pageIndex,pageSize);
		QueryWrapper<LoginLog> wrapper = new QueryWrapper<>();

		wrapper.eq("uid",uid);
		IPage<LoginLog> page=loginLogMapper.selectPage(page_,wrapper);
		return page.getRecords();
	}
	
	// 批量删除登录日志信息
	@Override
	public boolean removeLoginlog(String logId) {
		String[] id = logId.split(",");
		
		int[] lid = new int[id.length];
		for(int i = 0 ;i < id.length; i++){
			lid[i] = Integer.parseInt(id[i]);
		}
		
		return loginLogMapper.deleteBatchIds(Arrays.asList(lid))!=-1;
	}

}
